package com.balance.anole.erp.security;

import com.balance.anole.admin.service.AdminService;
import com.balance.anole.admin.to.AgentTO;
import com.balance.anole.admin.to.LoginUserTO;
import com.balance.anole.admin.to.StaffInfoTO;
import com.balance.anole.erp.security.exception.NoLoginUserInSessionException;
import com.balance.anole.erp.security.login.model.RetailLoginUser;
import com.balance.anole.erp.util.ServicesCenter;

public class SessionManager {

	private static AdminService as = ServicesCenter.getServicesCenter().getService(AdminService.class);

	private RetailLoginUser loginUser;

	private static SessionManager instance = new SessionManager();

	private SessionManager(){

	}

	public static SessionManager getInstance(){
		return instance;
	}
	public LoginUserTO getLoginUser(){
		if(this.loginUser == null){
			throw new NoLoginUserInSessionException();
			/*StaffInfoTO staff = as.getStaffInfo("RT00050001");
			LoginUserTO user = new LoginUserTO();
			user.setStaffInfo(staff);
			setLoginUser(user);*/
		}
		return loginUser.getLoginUserTO();
	}

	public RetailLoginUser getRetailLoginUser(){
		if(this.loginUser == null){
			throw new NoLoginUserInSessionException();
			/*StaffInfoTO staff = as.getStaffInfo("RT00050001");
			LoginUserTO user = new LoginUserTO();
			user.setStaffInfo(staff);
			setLoginUser(user);*/
		}
		return loginUser;
	}

	public void setLoginUser(RetailLoginUser loginUser){
		this.loginUser = loginUser;
	}

	public static StaffInfoTO getAuditor(){
		return as.getStaffInfoByStaffId("SD00010001");
	}

	public static AgentTO getDefaultAgent(){
		return as.getAgentTOByAgentId("TD0001");
	}

	public static AgentTO getDefaultSupplier(){
		return as.getAgentTOByAgentId("GB0001");
	}

	public void logout(){
		setLoginUser(null);
		//TODO: call server service to remove session trace in remote database.
	}

}
